#!/usr/bin/rexx
main:
    call Run_iwlist
    call BuildOutputIntermed
    call BuildOutputFinal
    exit

Run_iwlist:
    'sudo iwlist scan > iwlist.out'
    return

BuildOutputIntermed:
    'rm iwlist2.out'
    do forever
        FileLine=linein('iwlist.out')
        if FileLine='' then leave
        if pos('ESSID',FileLine) > 1 then x=lineout('iwlist2.out',FileLine)
        if pos('Frequency',FileLine) > 1 then x=lineout('iwlist2.out',FileLine)
        if pos('Quality',FileLine) > 1 then x=lineout('iwlist2.out',FileLine)
        end
    return

BuildOutputFinal:
    say '=========== Active Wifi routers ============='
    say '------------------------------------ ---  ---'    
    say 'ESSID                                Chn  dBm'
    say '------------------------------------ ---  ---'
    do forever
        do 3
            FileLine2=linein('iwlist2.out')
            if FileLine2='' then leave
            parse value FileLine2 with dummy1 '"' ESSID '"'
            ESSID=substr(ESSID,1,37)
            FileLine2=linein('iwlist2.out')
            Chn=word(FileLine2, 4)
            Chn=strip(Chn,'T',')')
            Chn=substr(Chn,1,3)
            FileLine2=linein('iwlist2.out')
            dBm2=word(FileLine2,3)
            parse value dBm2 with dummy2 '=' dBm
            say ESSID Chn dBm
            end
        if FileLine2='' then leave
        end
    return

